package com.lxk.service.impl;

import com.lxk.dao.*;
import com.lxk.pojo.*;
import com.lxk.service.GoodsService;
import com.lxk.util.exception.GlobalException;
import com.lxk.vo.GoodsNormalModel;
import com.lxk.vo.GoodsTicketModel;
import com.lxk.vo.PageModel;
import com.lxk.vo.result.CodeMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author JohnSoybean
 * @create 2020/3/23 15:36
 * @description
 */
@Service
@Transactional
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper gm;
    @Autowired
    private GoodsStatMapper gsm;
    @Autowired
    private GoodsComkeyMapper gcm;
    @Autowired
    private OrdersMapper om;
    @Autowired
    private TicketMapper tm;
    @Autowired
    private AsMyMapper mm;

    public Integer getDownGoodsTotalPages(Integer num) {
        Integer count = mm.getDownGoodsNum();
        Integer totalPages = count%num==0 ? count/num : count/num+1;
        return totalPages;
    }

    public PageModel<GoodsNormalModel> selDownGoodsPage(Integer pageIndex, Integer num, Integer totalPages, Integer sort, Integer order) {
        int start = (pageIndex-1)*num;
        List<GoodsNormalModel> list = mm.selDownGoodsPage(start, num, sort, order);
        PageModel<GoodsNormalModel> model = new PageModel<GoodsNormalModel>();
        model.setList(list);
        model.setNum(num);
        model.setPageIndex(pageIndex);
        model.setTotalPages(totalPages);
        return model;
    }

    public GoodsNormalModel selNormalGoodsById(Integer goodsId) {
        GoodsNormalModel model = mm.selNormalGoodsById(goodsId);
        return model;
    }

    public Integer getNormalGoodsTotalPages(Integer num) {
        Integer count = mm.getNormalGoodsNum();
        Integer totalPages = count%num==0 ? count/num : count/num+1;
        return totalPages;
    }

    public PageModel<GoodsNormalModel> selNormalGoodsPage(Integer pageIndex, Integer num, Integer totalPages, Integer sort, Integer order) {
        int start = (pageIndex-1)*num;
        List<GoodsNormalModel> list = mm.selNormalGoodsPage(start, num, sort, order);
        PageModel<GoodsNormalModel> model = new PageModel<GoodsNormalModel>();
        model.setList(list);
        model.setNum(num);
        model.setPageIndex(pageIndex);
        model.setTotalPages(totalPages);
        return model;
    }

    public Integer addNormalGoods(Goods goods, Integer isUpper) {
        goods.setGoodsTime(new Date());
        goods.setModifyTime(new Date());
        goods.setIsTicket(0);
        int ins = gm.insertSelective(goods);
        if(ins==0) {
            throw new GlobalException(CodeMsg.ADD_GOODS_ERROR);
        }
        Integer goodsId = goods.getGoodsId();
        GoodsStat goodsStat = new GoodsStat();
        goodsStat.setIsUpper(isUpper);
        goodsStat.setGoodsId(goodsId);
        int insStat = gsm.insertSelective(goodsStat);
        if(insStat==0) {
            throw new GlobalException(CodeMsg.ADD_GOODS_ERROR);
        }
        GoodsComkey goodsComkey = new GoodsComkey();
        goodsComkey.setGoodsId(goodsId);
        int insComkey = gcm.insertSelective(goodsComkey);
        if(insComkey==0) {
            throw new GlobalException(CodeMsg.ADD_GOODS_ERROR);
        }
        return 1;
    }

    public Integer modifyGoodsInfo(Goods goods) {
        int upd = gm.updateByPrimaryKeySelective(goods);
        return upd;
    }

    public void modifyTicket(Ticket ticket) {
        TicketExample example = new TicketExample();
        TicketExample.Criteria criteria = example.createCriteria();
        criteria.andGoodsIdEqualTo(ticket.getGoodsId());
        tm.updateByExampleSelective(ticket, example);
    }

    public void addTicket(Ticket ticket) {
        tm.insertSelective(ticket);
    }

    public Integer shiftGoodsUpper(Integer goodsId, Integer isUpper) {
        GoodsStatExample example = new GoodsStatExample();
        GoodsStatExample.Criteria criteria = example.createCriteria();
        criteria.andGoodsIdEqualTo(goodsId);
        List<GoodsStat> list = gsm.selectByExample(example);
        if(list.size()>0){
            GoodsStat goodsStat = list.get(0);
            goodsStat.setIsUpper(isUpper);
            int upd = gsm.updateByPrimaryKeySelective(goodsStat);
            if(upd==0){
                throw new GlobalException(CodeMsg.MYSQL_CRUD_FAIL);
            }
        }else{
            throw new GlobalException(CodeMsg.SHIFT_UPPER_ERROR);
        }
        return 1;
    }

    public Integer deleteGoods(Integer goodsId) {
        GoodsStatExample example = new GoodsStatExample();
        GoodsStatExample.Criteria criteria = example.createCriteria();
        criteria.andGoodsIdEqualTo(goodsId);
        List<GoodsStat> goodsStats = gsm.selectByExample(example);
        if (goodsStats.size() == 0) {
            throw new GlobalException(CodeMsg.NO_SUCH_GOODS);
        }
        GoodsStat stat = goodsStats.get(0);
        stat.setIsUpper(2);
        int upd = gsm.updateByPrimaryKeySelective(stat);
        return upd;
    }

    public Integer deleteShopGoods(Integer shopId) {
        GoodsExample ge =  new GoodsExample();
        GoodsExample.Criteria gec = ge.createCriteria();
        gec.andShopIdEqualTo(shopId);
        List<Goods> goodsList = gm.selectByExample(ge);
        if (goodsList.size() > 0) {
            List<Integer> idList = new ArrayList<>();
            for (Goods goods : goodsList) {
                idList.add(goods.getGoodsId());
            }
            GoodsStatExample example = new GoodsStatExample();
            GoodsStatExample.Criteria criteria = example.createCriteria();
            criteria.andGoodsIdIn(idList);
            GoodsStat stat = new GoodsStat();
            stat.setIsUpper(2);
            int upd = gsm.updateByExampleSelective(stat, example);
            return upd;
        }else {
            return 1;
        }

    }

    public GoodsTicketModel selTicketGoodsById(Integer goodsId) {
        GoodsTicketModel model = mm.selTicketGoodsById(goodsId);
        return model;
    }

    public Integer getTicketGoodsTotalPages(Integer num) {
        Integer count = mm.getTicketGoodsNum();
        Integer totalPages = count%num==0 ? count/num : count/num+1;
        return totalPages;
    }

    public PageModel<GoodsTicketModel> selTicketGoodsPage(Integer pageIndex, Integer num, Integer totalPages, Integer sort, Integer order) {
        int start = (pageIndex-1)*num;
        List<GoodsTicketModel> list = mm.selTicketGoodsPage(start, num, sort, order);
        PageModel<GoodsTicketModel> model = new PageModel<GoodsTicketModel>();
        model.setList(list);
        model.setNum(num);
        model.setPageIndex(pageIndex);
        model.setTotalPages(totalPages);
        return model;
    }

    public Integer addTicketGoods(Goods goods, Integer isUpper) {
        goods.setGoodsTime(new Date());
        goods.setModifyTime(new Date());
        goods.setIsTicket(1);
        int ins = gm.insertSelective(goods);
        if(ins==0) {
            throw new GlobalException(CodeMsg.ADD_GOODS_ERROR);
        }
        Integer goodsId = goods.getGoodsId();
        GoodsStat goodsStat = new GoodsStat();
        goodsStat.setIsUpper(isUpper);
        goodsStat.setGoodsId(goodsId);
        int insStat = gsm.insertSelective(goodsStat);
        if(insStat==0) {
            throw new GlobalException(CodeMsg.ADD_GOODS_ERROR);
        }
        GoodsComkey goodsComkey = new GoodsComkey();
        goodsComkey.setGoodsId(goodsId);
        int insComkey = gcm.insertSelective(goodsComkey);
        if(insComkey==0) {
            throw new GlobalException(CodeMsg.ADD_GOODS_ERROR);
        }
        return 1;
    }

    public String getGoodsDetailText(Integer goodsId) {
        Goods goods = gm.selectByPrimaryKey(goodsId);
        if(goods==null) {
            throw new GlobalException(CodeMsg.NO_SUCH_GOODS);
        }
        String detail = goods.getGoodsDetail();
        return detail;
    }

    public Integer updGoodsDetailText(Integer goodsId, String goodsDetail) {
        Goods goods = gm.selectByPrimaryKey(goodsId);
        if (goods==null) {
            throw new GlobalException(CodeMsg.NO_SUCH_GOODS);
        }
        goods.setGoodsDetail(goodsDetail);
        goods.setModifyTime(new Date());
        int upd = gm.updateByPrimaryKeyWithBLOBs(goods);
        if(upd==0) {
            throw new GlobalException(CodeMsg.MYSQL_CRUD_FAIL);
        }
        return 1;
    }


    public Integer addGoodsStock(Integer goodsId, Integer goodsCount) {
        Goods goods = gm.selectByPrimaryKey(goodsId);
        Integer goodsStock = goods.getGoodsStock();
        Integer goodsSales = goods.getGoodsSales();
        goodsStock = goodsStock + goodsCount;
        goods.setGoodsStock(goodsStock);
        goodsSales = goodsSales - goodsCount;
        goods.setGoodsSales(goodsSales);
        int ins = gm.updateByPrimaryKeySelective(goods);
        if(ins==1) {
            return 1;
        }else {
            throw new GlobalException(CodeMsg.CHANGE_STOCK_FAIL);
        }
    }

    public Integer getActivityGoodsTotalPages(Integer num) {
        Integer count = mm.getGoodsForActivityNum();
        Integer totalPages = count%num==0 ? count/num : count/num+1;
        return totalPages;
    }

    public PageModel<GoodsNormalModel> selActivityGoodsPage(Integer pageIndex, Integer num, Integer totalPages, Integer sort, Integer order) {
        int start = (pageIndex-1)*num;
        List<GoodsNormalModel> list = mm.selGoodsForActivityPage(start, num, sort, order);
        PageModel<GoodsNormalModel> model = new PageModel<GoodsNormalModel>();
        model.setList(list);
        model.setNum(num);
        model.setPageIndex(pageIndex);
        model.setTotalPages(totalPages);
        return model;
    }

    public Goods selById(Integer goodsId) {
        Goods goods = gm.selectByPrimaryKey(goodsId);
        return goods;
    }

}
